package com.hl.unittestlearn.presenter;

import android.util.Log;

import com.hl.unittestlearn.IView;
import com.hl.unittestlearn.http.APIService;
import com.hl.unittestlearn.http.bean.LoginResult;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;



public class Presenter{
    private IView view;
    private APIService apiManager;

    public Presenter(IView view,APIService apiService) {
        this.view = view;
        this.apiManager = apiService;
    }

    public void login(String username,String password)
    {
        apiManager.login(username,password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginResult>() {
                    @Override
                    public void onCompleted() {
                        System.out.println("onCompleted.");
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();

                        view.loginFailure();
                    }

                    @Override
                    public void onNext(LoginResult loginResult) {
                        System.out.println("onNext.");
                        Log.d("Tag", "登录成功");
                        view.updateView(loginResult.getMessage());
                    }
                });
    }

}
